<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	class Manage_news extends CI_Controller{

	 	public function __construct(){
 		parent::__construct();
 		$this->load->library('session');
 		$this->load->library('form_validation');
 		$this->load->library('cismarty');
 		$this->load->model("apanel/template");
 		$this->load->model("apanel/db_news");
 		$this->load->library('pagination');
 	}
 	
 	public function index(){
 		redirect('manage_news/news_list');
 	}
 	
 	public function news_list($st=""){
 		if($this->session->userdata('sess_login') == null)
 			redirect('apanel');
 		list($item,$num,$page,$total) = $this->db_news->news_item_list();
 		$this->cismarty->assign("Num_Rows",$num);
 		$this->cismarty->assign("item",$item);
 		$this->cismarty->assign("pagelink",$page);
 		$this->cismarty->assign("total",$total);
 		if($st == "success"){
 			$this->cismarty->assign("status","<div class=\"mws-form-message success message\">Success</div>");
 		}
 		
 		//== template
 		$this->cismarty->assign("header",$this->template->header());
 		$this->cismarty->assign("menu",$this->template->menu(3,2));
 		$this->cismarty->assign("footer",$this->template->footer());
 		$this->cismarty->assign("url",base_url());
 		$this->cismarty->assign("form_open",form_open('manage_news/news_item_delAll'));
 		$this->cismarty->assign("form_close",form_close());
 		$this->cismarty->display('apanel/news/news_list.html');
 	}
 	
 	public function news_item_add(){
 		if($this->session->userdata('sess_login') == null)
 			redirect('apanel');
 		if($this->input->post('bntSubmit') != null){
 			$this->form_validation->set_rules("status","status","required");
 			$this->form_validation->set_rules("title_en","name","required");
 			$this->form_validation->set_rules("customer","Customer","required");
 			$this->form_validation->set_rules("title_cn","name","trim");
 			$this->form_validation->set_rules("title_th"," name","trim");
 			$this->form_validation->set_rules("desc_en","Description","trim");
 			$this->form_validation->set_rules("desc_cn","Description","trim");
 			$this->form_validation->set_rules("desc_th","Description","trim");
 			
 			if($this->form_validation->run() == FALSE){
 				$this->cismarty->assign("customer",set_value("customer"));
 				$this->cismarty->assign("title_en",set_value('title_en'));
 				$this->cismarty->assign("title_cn",set_value('title_cn'));
 				$this->cismarty->assign("title_th",set_value('title_th'));
 				$this->cismarty->assign("desc_en",set_value('desc_en'));
 				$this->cismarty->assign("desc_cn",set_value('desc_cn'));
 				$this->cismarty->assign("desc_th",set_value('desc_th'));
 					
 				//set error
 				$this->cismarty->assign("errorcustomer",form_error('customer'));
 				$this->cismarty->assign("errortitle_en",form_error('title_en'));
 				$this->cismarty->assign("errorstatus",form_error('status'));
 			}else {
 				$error = false;
 				if($_FILES['pic']['tmp_name'] != ""){
 					$config['upload_path'] = 'media/news/';
 					$config['allowed_types'] = 'gif|jpg|png';
 					$config['max_size']	= '1024'; // kb
 					$this->load->library('upload', $config);
 					if ( $this->upload->do_upload('pic'))
 					{
 						$data =  $this->upload->data();
 						$picname = date('YmdHis').$data['file_ext'];
 						rename($data['full_path'], $data['file_path'].$picname);
 						$config['image_library'] ='gd2';
 						$config['source_image'] =  $data['file_path'].$picname;
 						$config['width']	= 256;
 						$config['height'] = 220;
 						$this->load->library('image_lib', $config);
 						$this->image_lib->resize();
 					}else{
 						//$pic_error = array('error' => $this->upload->display_errors());
 						$this->cismarty->assign("errorpic",$this->upload->display_errors());
 						$error = true;
 					}
 				}
 				if(!$error){
	 				$data = array(
	 						"customer_pkid"=>$this->input->post("customer"),
	 						"activity_type"=>$this->input->post("activity_type"),
	 						"title_en"=>trim($this->input->post('title_en')),
	 						"title_zh_CN"=>trim($this->input->post('title_cn')),
	 						"title_th"=>trim($this->input->post('title_th')),
	 						"content_en"=>addslashes(trim($this->input->post('desc_en'))),
	 						"content_zh_CN"=>addslashes(trim($this->input->post('desc_cn'))),
	 						"content_th"=>addslashes(trim($this->input->post('desc_th'))),
	 						"img"=>$picname,
	 						"status"=>$this->input->post('status'),
	 						"created_by"=>$this->session->userdata("sess_username"),
	 						"created_date"=>date("Y-m-d H:i:s")
	 				);
	 				$res = $this->db->insert('mobile_activity',$data);
	 				if($res) redirect('manage_news/news_list/success');
	 				else  redirect('manage_news/news_list/fail');
 				}
 			}
 		}//end post
 		
 		
 		$this->cismarty->assign("tiny",$this->template->tinymce());
 		$this->cismarty->assign("customer",$this->db_news->customer_list());
 		$this->cismarty->assign("list_activity",$this->db_news->activity_type());
 		//== template
 		$this->cismarty->assign("header",$this->template->header());
 		$this->cismarty->assign("menu",$this->template->menu(3,2));
 		$this->cismarty->assign("footer",$this->template->footer());
 		$this->cismarty->assign("url",base_url());
 		$this->cismarty->assign("form_open",form_open_multipart('manage_news/news_item_add'));
 		$this->cismarty->assign("form_close",form_close());
 		$this->cismarty->display('apanel/news/news_item_add.html');
 	}
 	
 	public function news_item_edit($id){
 		if($this->session->userdata('sess_login') == null)
 			redirect('apanel');
 		if($this->input->post('bntSubmit') != null){
 			$this->form_validation->set_rules("status","status","required");
 			$this->form_validation->set_rules("title_en","name","required");
 			$this->form_validation->set_rules("title_cn","name","trim");
 			$this->form_validation->set_rules("title_th"," name","trim");
 			$this->form_validation->set_rules("customer","customer","required");
 			$this->form_validation->set_rules("desc_en","Description","trim");
 			$this->form_validation->set_rules("desc_cn","Description","trim");
 			$this->form_validation->set_rules("desc_th","Description","trim");
 			
 			if($this->form_validation->run() == FALSE){
 				$this->cismarty->assign("title_en",set_value('title_en'));
 				$this->cismarty->assign("title_cn",set_value('title_cn'));
 				$this->cismarty->assign("title_th",set_value('title_th'));
 				$this->cismarty->assign("customer",set_value('customer'));
 				$this->cismarty->assign("desc_en",set_value('desc_en'));
 				$this->cismarty->assign("desc_cn",set_value('desc_cn'));
 				$this->cismarty->assign("desc_th",set_value('desc_th'));
 					
 				//set error
 				$this->cismarty->assign("errortitle_en",form_error('title_en'));
 				$this->cismarty->assign("errorcustomer",form_error('customer'));
 				$this->cismarty->assign("errordesc_en",form_error('desc_en'));
 				$this->cismarty->assign("errorstatus",form_error('status'));
 			}else {
 				$error = false;
 				if($_FILES['pic']['tmp_name'] != ""){
 					$config['upload_path'] = 'media/news/';
 					$config['allowed_types'] = 'gif|jpg|png';
 					$config['max_size']	= '1024'; // kb
 					$this->load->library('upload', $config);
 					if ( $this->upload->do_upload('pic'))
 					{
 						$data =  $this->upload->data();
 						$picname = date('YmdHis').$data['file_ext'];
 						rename($data['full_path'], $data['file_path'].$picname);
 						$config['image_library'] ='gd2';
 						$config['source_image'] =  $data['file_path'].$picname;
 						$config['width']	= 256;
 						$config['height'] = 220;
 						$this->load->library('image_lib', $config);
 						$this->image_lib->resize();
 						$uppic = array("img"=>$picname);
 						$this->db->where("pkid",$id);
 						$this->db->update('mobile_activity',$uppic);
 					}else{
 						//$pic_error = array('error' => $this->upload->display_errors());
 						$this->cismarty->assign("errorpic",$this->upload->display_errors());
 						$error = true;
 					}
 				}
 				if(!$error){
	 				$data = array(
	 						"customer_pkid"=>trim($this->input->post('customer')),
	 						"activity_type"=>$this->input->post("activity_type"),
	 						"title_en"=>trim($this->input->post('title_en')),
	 						"title_zh_CN"=>trim($this->input->post('title_cn')),
	 						"title_th"=>trim($this->input->post('title_th')),
	 						"content_en"=>addslashes(trim($this->input->post('desc_en'))),
	 						"content_zh_CN"=>addslashes(trim($this->input->post('desc_cn'))),
	 						"content_th"=>addslashes(trim($this->input->post('desc_th'))),
	 						"status"=>$this->input->post('status'),
	 						"updated_by"=>$this->session->userdata("sess_username"),
	 						"updated_date"=>date("Y-m-d H:i:s")
	 				);
	 				$this->db->where("pkid",$id);
	 				$res = $this->db->update('mobile_activity',$data);
	 				if($res) redirect('manage_news/news_list/success');
	 				else  redirect('manage_news/news_list/fail');
 				}
 			}
 		}else{
 			$this->db->where("pkid",$id);
 			$res = $this->db->get("mobile_activity");
 			$row = $res->row();
 			$this->cismarty->assign("title_en",$row->title_en);
 			$this->cismarty->assign("title_cn",$row->title_zh_CN);
 			$this->cismarty->assign("title_th",$row->title_th);
 			$this->cismarty->assign("customer",$this->db_news->customer_list($row->customer_pkid));
 			$this->cismarty->assign("desc_en",stripslashes($row->content_en));
 			$this->cismarty->assign("desc_cn",stripslashes($row->content_zh_CN));
 			$this->cismarty->assign("desc_th",stripslashes($row->content_th));
 			if(trim($row->img) != ""){
 				$this->cismarty->assign("img",'<img src="'.base_url().'media/news/'.$row->img.'"><br>');
 			}
 			if($row->status == 1) $this->cismarty->assign("sa",'checked="checked"');
 			else $this->cismarty->assign("sd",'checked="checked"');
 			$activity_type = $row->activity_type;
 		}
 						
 		$this->cismarty->assign("tiny",$this->template->tinymce());
 		$this->cismarty->assign("list_activity",$this->db_news->activity_type($activity_type));
 		//== template
 		$this->cismarty->assign("header",$this->template->header());
 		$this->cismarty->assign("menu",$this->template->menu(3,2));
 		$this->cismarty->assign("footer",$this->template->footer());
 		$this->cismarty->assign("url",base_url());
 		$this->cismarty->assign("form_open",form_open_multipart("manage_news/news_item_edit/$id"));
 		$this->cismarty->assign("form_close",form_close());
 		$this->cismarty->display('apanel/news/news_item_edit.html');
 	}
 	
 	public function news_item_del($id){
 		if($this->session->userdata('sess_login') == null)
 			redirect('apanel');
 		if($id != ""){
 			$this->db->where('pkid',$id);
 			$res = $this->db->delete("mobile_activity");
 			if($res) redirect('manage_news/news_list/success');
 		}
 	}
 	
 	public function news_item_delAll(){
 		$check = $this->input->post('check');
 		if($check != ""){
 			foreach ($check as $index){
 				$this->db->where('pkid',$index);
 				$res = $this->db->delete("mobile_activity");
 			}
 		}
 		redirect('manage_news/news_list/success');
 	}
		
} //end class
?>